package com.yogee.topseller.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.model.bean.InterestLabelBean;
import com.yogee.topseller.application.TopellerApplication;
import com.yogee.topseller.bean.AddFastQueryBean;
import com.yogee.topseller.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static com.igexin.push.core.a.j;

/**
 * Created by Administrator on 2017/6/20.
 */

public class FlowLayout extends ViewGroup {
    private LayoutInflater mInflater;
    private boolean isColorful;
    private Context mContext;
    //存储子View的LayoutParams
    MarginLayoutParams params = null;

    public FlowLayout(Context context) {
        this(context, null);
        mContext = context;
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        mContext = context;
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mInflater = LayoutInflater.from(getContext());
        mContext = context;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        // wrapContent
        int width = 0;
        int height = 0;

        // 记录每一行的宽和高
        int lineWidth = 0;
        int lineHeight = 0;

        // 得到内部元素的个数    `
        int count = getChildCount();


        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            //获取子View的LayoutParams，(子View的LayoutParams的对象类型，取决于其ViewGroup的generateLayoutParams()方法的返回的对象类型，这里返回的是MarginLayoutParams)
            params = (MarginLayoutParams) child.getLayoutParams();
            params.setMargins(dp2px(7), dp2px(7), dp2px(7), dp2px(7));
            // 测量子View的宽和高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            // 得到LayoutParams
            MarginLayoutParams lp = (MarginLayoutParams) child
                    .getLayoutParams();
            // 子view的占据的宽度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin
                    + lp.rightMargin;
            // 子view占据的高度
            int childHeight = child.getMeasuredHeight() + lp.topMargin
                    + lp.bottomMargin;
            // 换行
            if (lineWidth + childWidth > sizeWidth - getPaddingLeft()
                    - getPaddingRight()) {

                // 对比得到最大的宽度
                width = Math.max(width, lineWidth);
                // 重置lineWidth
                lineWidth = childWidth;
                // 记录行高
                height += lineHeight;
                lineHeight = childHeight;
            } else {
                // 未换行
                // 叠加行宽
                lineWidth += childWidth;
                // 得到当前最大高度
                lineHeight = Math.max(lineHeight, childHeight);
            }
            // 最后一个控件
            if (i == count - 1) {
                width = Math.max(lineWidth, width);
                height += lineHeight;
            }
        }
//        Log.i("test", "sizeWidth" + sizeWidth);
//        Log.i("test", "sizeHeight" + sizeHeight);

        setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth
                        : width + getPaddingLeft() + getPaddingRight(),
                modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height
                        + getPaddingTop() + getPaddingBottom());
//        setPadding(dp2px(5), dp2px(5), dp2px(5), dp2px(5));
    }

    // 储存所有的View
    private ArrayList<ArrayList<View>> mAllViews = new ArrayList<>();
    // 储存每一行的高度
    private ArrayList<Integer> mLineHeight = new ArrayList<>();

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub

        // 清除一下list集合
        mAllViews.clear();
        mLineHeight.clear();

        // 得到viewGroup当前宽度
        int width = getWidth();

        int lineWidth = 0;
        int lineHeight = 0;

        ArrayList<View> lineViews = new ArrayList<>();

        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);

            MarginLayoutParams lp = (MarginLayoutParams) child
                    .getLayoutParams();
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();

            // 如果需要换行
            if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width
                    - getPaddingLeft() - getPaddingRight()) {
                // 记录当前行高
                mLineHeight.add(lineHeight);
                // 记录当前行的view
                mAllViews.add(lineViews);

                // 重置行宽和行高
                lineWidth = 0;
                lineHeight = childHeight + lp.topMargin + lp.bottomMargin;
                // 重置lineViews集合
                lineViews = new ArrayList<>();
            }
            lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
            lineHeight = Math.max(lineHeight, childHeight + lp.topMargin
                    + lp.bottomMargin);
            lineViews.add(child);
        }
        // 处理最后一行
        mLineHeight.add(lineHeight);
        mAllViews.add(lineViews);

        // 设置子view的位置
        int left = getPaddingLeft();
        int top = getPaddingTop();

        // 有多少行
        int lineNum = mLineHeight.size();

        for (int i = 0; i < lineNum; i++) {

            // 获取当前行的view
            lineViews = mAllViews.get(i);
            // 当前行高
            lineHeight = mLineHeight.get(i);
            int lineViewSize = lineViews.size();
            for (int j = 0; j < lineViewSize; j++) {
                View child = lineViews.get(j);
                // 判断子view的状态
                if (child.getVisibility() == View.GONE) {
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) child
                        .getLayoutParams();
                int childWidth = child.getMeasuredWidth();
                int childHeight = child.getMeasuredHeight();

                int lc = left + lp.leftMargin;
                int tc = top + lp.topMargin;
                int rc = lc + childWidth;
                int bc = tc + childHeight;

                // 为子view布局
                child.layout(lc, tc, rc, bc);

                // 同一行view坐起点坐标的变换
                left += childWidth + lp.leftMargin + lp.rightMargin;
            }
            // 换行时将left重置
            left = getPaddingLeft();
            // top要加上上一行的行高
            top += lineHeight;
        }
    }

    /**
     * 默认返回的LayoutParams
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        // TODO Auto-generated method stub
        return new MarginLayoutParams(getContext(), attrs);
    }

    /**
     * 设置数据 默认字体颜色和边框颜色一直
     */
    public void setData(final List<InterestLabelBean.ListBean.LabelListBean> strings, ArrayList<InterestLabelBean.ListBean.LabelListBean> mSelectBean) {

        int count = strings.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_textview, this,
                    false);
            tv.setText(strings.get(i).getName());
            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onTagClickListener != null)
                        onTagClickListener.TagClick(strings.get(j), tv);
                }
            });
            if (isColorful) {
                Random random = new Random();
                int ranColor = 0xff000000 | random.nextInt(0x00ffffff);
                tv.setBackgroundColor(ranColor);
            }
            for (InterestLabelBean.ListBean.LabelListBean bean : mSelectBean) {
                if (strings.get(i).getId().equals(bean.getId())) {
                    tv.setTextColor(mContext.getResources().getColor(R.color.white));
                    tv.setBackgroundResource(R.drawable.corens_100_style_select_new);
                }
            }

            this.addView(tv);
        }
    }

    /**
     * 设置数据 默认字体颜色和边框颜色一直
     */
    public  ArrayList<TextView> setDataDefaultSelect(final List<InterestLabelBean.ListBean.LabelListBean> strings) {

        ArrayList<TextView> mSelectView = new ArrayList<>();
        int count = strings.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_textview, this,
                    false);
            tv.setText(strings.get(i).getName());
            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onTagClickListener != null)
                        onTagClickListener.TagClick(strings.get(j), tv);
                }
            });
            tv.setTextColor(mContext.getResources().getColor(R.color.select_color));
            tv.setBackgroundResource(R.drawable.corens_100_style_select);
            Drawable dra = ContextCompat.getDrawable(mContext,R.drawable.select);
            dra.setBounds(8,0,0,0);
            tv.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(mContext,R.drawable.select),null,null,null);
            mSelectView.add(tv);
            this.addView(tv);
        }
        return mSelectView;
    }

    /**
     * 设置数据 默认字体颜色和边框颜色一直
     */
    public void setData(final List<InterestLabelBean.ListBean.LabelListBean> strings) {

        int count = strings.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_textview, this,
                    false);
            tv.setText(strings.get(i).getName());
            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onTagClickListener != null)
                        onTagClickListener.TagClick(strings.get(j), tv);
                }
            });
            if (isColorful) {
                Random random = new Random();
                int ranColor = 0xff000000 | random.nextInt(0x00ffffff);
                tv.setBackgroundColor(ranColor);
            }
            this.addView(tv);
        }
    }

    /**
     * 设置数据 默认字体颜色和边框颜色一直
     */
    public void setData(final List<InterestLabelBean.ListBean.LabelListBean> strings,InterestLabelBean.ListBean.LabelListBean bean) {

        int count = strings.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_textview, this,
                    false);
            tv.setText(strings.get(i).getName());
            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onTagClickListener != null)
                        onTagClickListener.TagClick(strings.get(j), tv);
                }
            });
            if (isColorful) {
                Random random = new Random();
                int ranColor = 0xff000000 | random.nextInt(0x00ffffff);
                tv.setBackgroundColor(ranColor);
            }
            if(strings.get(i)==bean){
                tv.setTextColor(mContext.getResources().getColor(R.color.select_color));
                tv.setBackgroundResource(R.drawable.corens_100_style_select);
                Drawable dra = ContextCompat.getDrawable(mContext,R.drawable.select);
                dra.setBounds(8,0,0,0);
                tv.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(mContext,R.drawable.select),null,null,null);

            }
            this.addView(tv);
        }
    }

    /**
     * 设置数据 默认字体颜色和边框颜色一直
     */
    public void setDefaultData(final List<String> strings) {

        int count = strings.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_textview, this,
                    false);
            tv.setText(strings.get(i));
            this.addView(tv);
        }
    }

    /**
     * 设置数据 默认字体颜色和边框颜色一直
     */
    public void setDefaultData1(final List<String> strings) {

        int count = strings.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_textview1, this,
                    false);
            tv.setText(strings.get(i));
            this.addView(tv);
        }
    }


    /**
     * 设置数据 默认字体颜色和边框颜色一直
     */
    public void setDataOther(final List<InterestLabelBean.ListBean.LabelListBean> strings) {

        int count = strings.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_textview, this,
                    false);
            tv.setText(strings.get(i).getName());
            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onTagClickListener != null)
                        onTagClickListener.TagClick(strings.get(j), tv);
                }
            });
            //设置字体颜色
            tv.setTextColor(Color.parseColor("#4a4a4a"));
            //设置边框样式
            GradientDrawable myGrad = (GradientDrawable) tv.getBackground();
            myGrad.setColor(mContext.getResources().getColor(R.color.colorAllBG));
            myGrad.setStroke(1, Color.parseColor("#cccccc"));
            myGrad.setCornerRadius(dp2px(10));
            this.addView(tv);
        }
    }


    /**
     * 设置灰色背景圆角 TextView
     *
     * @param strings
     */
    public void setDatas(final List<String> strings) {

        int count = strings.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_two_tv, this,
                    false);
            tv.setText(strings.get(i));

            //设置字体颜色
            tv.setTextColor(Color.parseColor("#ffffff"));
            //设置边框样式
            GradientDrawable myGrad = (GradientDrawable) tv.getBackground();
            myGrad.setColor(Color.parseColor("#9b9b9b"));
            myGrad.setStroke(1, Color.parseColor("#9b9b9b"));
            myGrad.setCornerRadius(dp2px(100));

            this.addView(tv);
        }
    }


    public void setDatasProduct(final List<String> strings) {

        int count = strings.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_two_tv_product, this,
                    false);
            tv.setText(strings.get(i));

//            //设置字体颜色
//            tv.setTextColor(Color.parseColor("#4a4a4a"));
//            //设置边框样式
//            GradientDrawable myGrad = (GradientDrawable) tv.getBackground();
//            myGrad.setColor(Color.parseColor("#9b9b9b"));
//            myGrad.setStroke(1, Color.parseColor("#9b9b9b"));
//            myGrad.setCornerRadius(dp2px(100));

            this.addView(tv);
        }
    }

    /**
     * 设置灰色背景圆角 TextView
     *
     * @param strings
     */
    public void setDefoutDatas(final List<String> strings) {
        int count = strings.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_three_tv, this,
                    false);
            tv.setText(strings.get(i));

            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    onLableClickListener.onLableClick(strings.get(j));
                }
            });
            this.addView(tv);
        }
    }

    /**
     * 设置灰色背景圆角 TextView
     *
     * @param strings
     */
    public void setDefoutDatas1(final List<String> strings) {
        int count = strings.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_textview, this,
                    false);
            tv.setText(strings.get(i));

            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    onLableClickListener.onLableClick(strings.get(j));
                }
            });
            this.addView(tv);
        }
    }

    /**
     * 设置灰色背景圆角 TextView
     */
    public void setHotDatas(final List<AddFastQueryBean.FootListBean> beanList) {
        int count = beanList.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_three_tv, this,
                    false);
            tv.setText(beanList.get(i).getName());

            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    onAddClickListener.onAddClick(beanList.get(j));
                }
            });
            this.addView(tv);
        }
    }
    /**
    * 带有状态的标签，热门高亮
    * @author hanyu
    * created by 2017/10/18 15:24
    */
    class HotData{
        public boolean isCheck;
        public AddFastQueryBean.FootListBean bean;
    }
    //所有标签，热门的有选中状态
    List<HotData> hotList = new ArrayList<>();
    /**
     * 设置全部标签回显
     */
    public void setHotDatasShow(final List<AddFastQueryBean.FootListBean> beanList, final List<AddFastQueryBean.LabelListBean> labelList) {
        //组装有标识的数据集合
        hotList.clear();
        for (int i = 0; i < beanList.size(); i++) {
            HotData data = new HotData();
            data.isCheck = false;
            data.bean = beanList.get(i);
            hotList.add(data);
        }
        //将所有标签里的热门标签改状态
        for (int i = 0; i < hotList.size(); i++) {
            for (int k = 0; k < labelList.size(); k++) {
                if (hotList.get(i).bean.getId().equals(labelList.get(k).getId())){
                    hotList.get(i).isCheck = true;
                }
            }
        }
        //视图里添加数据
        cleanTag();
        int count = hotList.size();
        for (int k = 0; k < count; k++) {
            final int j = k;
            if (hotList.get(k).isCheck){
                final View v = mInflater.inflate(R.layout.flowlayout_flash_layout, this,
                        false);
                TextView tv = (TextView) v.findViewById(R.id.flash_lable_tv);
                tv.setText(hotList.get(j).bean.getName());
                tv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onAddClickListener.onAddClick(beanList.get(j));
                    }
                });
                this.addView(v);
            }else{
                final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_three_tv, this,
                        false);
                tv.setText(beanList.get(k).getName());
                tv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onAddClickListener.onAddClick(beanList.get(j));
                        tv.setTextColor(mContext.getResources().getColor(R.color.colorStyle));
                        tv.setBackgroundResource(R.drawable.corens_1_style);
                    }
                });
                this.addView(tv);
            }
        }
    }


    /**
     * 设置全部标签回显
     */
    public void setHotDatasShow1(final List<AddFastQueryBean.FootListBean> beanList, final List<AddFastQueryBean.LabelListBean> labelList) {
        //组装有标识的数据集合
        hotList.clear();
        for (int i = 0; i < beanList.size(); i++) {
            HotData data = new HotData();
            data.isCheck = false;
            data.bean = beanList.get(i);
            hotList.add(data);
        }
        //将所有标签里的热门标签改状态
        for (int i = 0; i < hotList.size(); i++) {
            for (int k = 0; k < labelList.size(); k++) {
                if (hotList.get(i).bean.getId().equals(labelList.get(k).getId())){
                    hotList.get(i).isCheck = true;
                }
            }
        }
        //视图里添加数据
        cleanTag();
        int count = hotList.size();
        for (int k = 0; k < count; k++) {
            final int j = k;
            if (hotList.get(k).isCheck){
                final View v = mInflater.inflate(R.layout.flowlayout_flash_layout, this,
                        false);
                TextView tv = (TextView) v.findViewById(R.id.flash_lable_tv);
                tv.setText(hotList.get(j).bean.getName());
                tv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onAddClickListener.onAddClick(beanList.get(j));
                    }
                });
                this.addView(v);
            }else{
                final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_textview, this,
                        false);
                tv.setText(beanList.get(k).getName());
                tv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onAddClickListener.onAddClick(beanList.get(j));
                        tv.setTextColor(mContext.getResources().getColor(R.color.colorStyle));
                        tv.setBackgroundResource(R.drawable.corens_1_style);
                    }
                });
                this.addView(tv);
            }
        }
    }

    /**
     * 设置全部标签回显
     */
    public void setHotDatasShow2(final List<AddFastQueryBean.FootListBean> beanList, final List<AddFastQueryBean.LabelListBean> labelList) {
        //组装有标识的数据集合
        hotList.clear();
        for (int i = 0; i < beanList.size(); i++) {
            HotData data = new HotData();
            data.isCheck = false;
            data.bean = beanList.get(i);
            hotList.add(data);
        }
        //将所有标签里的热门标签改状态
        for (int i = 0; i < hotList.size(); i++) {
            for (int k = 0; k < labelList.size(); k++) {
                if (hotList.get(i).bean.getId().equals(labelList.get(k).getId())){
                    hotList.get(i).isCheck = true;
                }
            }
        }
        //视图里添加数据
        cleanTag();
        int count = hotList.size();
        for (int k = 0; k < count; k++) {
            final int j = k;
            if (hotList.get(k).isCheck){
//                final View v = mInflater.inflate(R.layout.flowlayout_flash_layout_new, this,
//                        false);
//                TextView tv = (TextView) v.findViewById(R.id.flash_lable_tv);
//                tv.setText(hotList.get(j).bean.getName());
//                tv.setOnClickListener(new OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        onAddClickListener.onAddClick(beanList.get(j));
//                    }
//                });
//                this.addView(v);
            }else{
                final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_textview, this,
                        false);
                tv.setText(beanList.get(k).getName());
                tv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(labelList!=null&&labelList.size()>=5){

                        }else{
//                            tv.setTextColor(mContext.getResources().getColor(R.color.colorStyle));
//                            tv.setBackgroundResource(R.drawable.corens_1_style_new);
                        }
                        onAddClickListener.onAddClick(beanList.get(j));

                    }
                });
                this.addView(tv);
            }
        }
    }

    /**
     * 设置灰色背景圆角 TextView
     */
    public void setDeleteDatas(final List<AddFastQueryBean.LabelListBean> labelListBeen) {
        cleanTag();
        int count = labelListBeen.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final View v = mInflater.inflate(R.layout.flowlayout_flash_layout, this,
                    false);
            final TextView tv = (TextView) v.findViewById(R.id.flash_lable_tv);

            tv.setText(labelListBeen.get(i).getName());

            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    onDeleteClickListener.onDeleteClick(labelListBeen.get(j));
                }
            });
            this.addView(v);
        }
    }
    /**
     * 设置灰色背景圆角 TextView
     */
    public void setDeleteDatas1(final List<AddFastQueryBean.LabelListBean> labelListBeen,final Context context) {
        cleanTag();
        int count = labelListBeen.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final View v = mInflater.inflate(R.layout.flowlayout_flash_layout_delete, this,
                    false);
            final TextView tv = (TextView) v.findViewById(R.id.flash_lable_tv);

            tv.setText(labelListBeen.get(i).getName());

            v.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    onDeleteClickListener.onDeleteClick(labelListBeen.get(j));
                }
            });
            final ImageView img_clear = (ImageView) v.findViewById(R.id.img_clear);

            img_clear.post(new Runnable() {
                @Override
                public void run() {
                    int tvwidth = tv.getWidth();
                    int imgwidth = img_clear.getWidth();
                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    lp.setMargins(tvwidth-imgwidth/2, 0, 0, 0);
                    img_clear.setLayoutParams(lp);
                    img_clear.setVisibility(View.VISIBLE);
                }
            });


            this.addView(v);
        }
    }

    /**
     * 设置灰色背景圆角 TextView
     */
    public void setFlashDetailData(final List<AddFastQueryBean.LabelListBean> labelListBeen) {
        cleanTag();
        int count = labelListBeen.size();
        for (int i = 0; i < count; i++) {
            final int j = i;
            final View v = mInflater.inflate(R.layout.flowlayout_flash_layout, this,
                    false);
            TextView tv = (TextView) v.findViewById(R.id.flash_lable_tv);
            tv.setText(labelListBeen.get(i).getName());

            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    onDeleteClickListener.onDeleteClick(labelListBeen.get(j));
                }
            });
            this.addView(v);
        }
    }

    /**
     * 设置数据
     */
//    public void setListData(List<String> list) {
//
//        int count = list.size();
//        for (int i = 0; i < count; i++) {
//            final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_textview, this,
//                    false);
//            tv.setText(list.get(i));
//            tv.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (onTagClickListener != null)
//                        onTagClickListener.TagClick(tv.getText().toString(), tv);
//                }
//            });
//
//            this.addView(tv);
//        }
//    }

//    /**
//     * 添加标签
//     *
//     * @param text
//     */
//    public void addTag(String text) {
//        final TextView tv = (TextView) mInflater.inflate(R.layout.flowlayout_textview, this,
//                false);
//        tv.setText(text);
//        tv.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (onTagClickListener != null)
//                    onTagClickListener.TagClick(tv.getText().toString(), tv);
//            }
//        });
//
//        if (isColorful) {
//            Random random = new Random();
//            int ranColor = 0xff000000 | random.nextInt(0x00ffffff);
//            tv.setBackgroundColor(ranColor);
//        }
//        this.addView(tv);
//    }

    /**
     * 设置多彩颜色
     *
     * @param isColorful
     */
    public void setColorful(boolean isColorful) {
        this.isColorful = isColorful;
    }

    /**
     * 删除所有标签
     */
    public void cleanTag() {
        this.removeAllViews();
    }

    public int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }


    private OnTagClickListener onTagClickListener;
    private OnDeleteClickListener onDeleteClickListener;
    private OnAddClickListener onAddClickListener;
    private OnLableClickListener onLableClickListener;

    private OnTagClickListener1 onTagClickListener1;

    public void setOnLableClickListener(OnLableClickListener onLableClickListener) {
        this.onLableClickListener = onLableClickListener;
    }

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        this.onDeleteClickListener = onDeleteClickListener;
    }

    public void setOnAddClickListener(OnAddClickListener onAddClickListener) {
        this.onAddClickListener = onAddClickListener;
    }

    public void setOnTagClickListener(OnTagClickListener onTagClickListener) {
        this.onTagClickListener = onTagClickListener;
    }
    public void setOnTagClickListener1(OnTagClickListener1 onTagClickListener) {
        this.onTagClickListener1 = onTagClickListener;
    }

    public interface OnTagClickListener1 {
        void TagClick(AddFastQueryBean.FootListBean bean, TextView textView);
    }

    public interface OnTagClickListener {
        void TagClick(InterestLabelBean.ListBean.LabelListBean bean, TextView textView);
    }

    public interface OnDeleteClickListener {
        void onDeleteClick(AddFastQueryBean.LabelListBean bean);
    }

    public interface OnAddClickListener {
        void onAddClick(AddFastQueryBean.FootListBean footListBean);
    }

    public interface OnLableClickListener {
        void onLableClick(String s);
    }

}
